package com.moma.chatting.core;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.moma.chatting.exception.*;

public class DynamicGroup {
	
	public DynamicGroup(){
		this.userMap = new HashMap<String, User>();
	}
	private UUID groupId;
	public void setGroupId(UUID value){
		this.groupId = value;
	}
	public UUID getGroupId(){
		return this.groupId;
	}
	
	private String name;
	public void setName(String value){
		this.name = value;
	}
	public String getName(){
		return this.name;
	}
	
	private HashMap<String, User> userMap;
//	private ArrayList<User> userList;
//	public void setUserMap(HashMap<String, User> value){
//		this.userMap = value;
//	}
//	public HashMap<String, User> getUserMap(){
//		return this.userMap;
//	}
	public User getUser(String userUniqueName){
		User user = this.userMap.get(userUniqueName);
		return user;
	}
	
	public User[] getAllUsers(){
		User[] userArray = new User[this.getUserCount()];
		this.userMap.values().toArray(userArray);
		return userArray;
	}
	
	public int getUserCount(){
		return this.userMap.size();
	}
	
	public boolean getIsAlive(){
		//boolean isAlive = true;
		int countOfUserNotOnline = 0;
		for (Map.Entry<String, User>  entry: this.userMap.entrySet()){
			User user = entry.getValue();
			if (!user.getIsOnline()){
				countOfUserNotOnline++;
				if (countOfUserNotOnline >= 2)
					return false;
			}
		}
		
		return true;
	}
	
	public void addUser(User user) throws DuplicationException{
		if (this.userMap.containsKey(user.getUniqueName()))
			throw new DuplicationException("user already exists in the group!");
		this.userMap.put(user.getUniqueName(), user);
//		this.userList.add(user);
	}
	
	public void removeUser(String userUniqueName) throws NotExistException{
		if (!this.userMap.containsKey(userUniqueName))
			throw new NotExistException("user not exists in the group!");
		this.userMap.remove(userUniqueName);
//		this.userList.remove(user);
	}
	
//	public void reportAlive(String userUniqueName) throws NotExistException{
//		if (!this.userMap.containsKey(userUniqueName))
//			throw new NotExistException("user not exists in the group!");
//		User user = this.userMap.get(userUniqueName);
//		user.reportOnline();
////		user1
//	}
}

